define(["PDUtilDir/grid","PDUtilDir/searchBlock","PDUtilDir/slidebar","PDUtilDir/fileupload/multiFileUpload","PDUtilDir/inputSelect","PDUtilDir/tool", "PDUtilDir/util"],
    function(Grid,SearchBlock,Slidebar,MFU,inputSelect,Tool,Util){
        function openDemoAForm(){
            openForm({
                body:"static/core/examples/demos/form/form/form.html",
                ctrl:"static/core/examples/demos/form/form/formCtrl",
                uuid:"uuid"
            });
        }

        function init(){
            selectAll();
            querySpots(null);
        }
        var initGrid = function (data) {
            SearchBlock.init('searchBlockId');
            //初始化
            tgrid = Grid({
                id:"DemoGirdDivId",
                pageSize:10,
                index:true,
                multi:true,
                pagination : true,
                cache:false,
                layout:[
                    {name:"门票代码",field:"code",sort:true,click:function(e){
                            var row = e.data.row;
                            saveOrUpdate(row);
                        }},
                    {name:"价格",field:"price"},
                    {name:"数量",field:"number",sort:true},
                    {name:"类型",field:"type",width:"10%",format:function (e) {
                            var type = e.row.type;
                            if(type==="D_SPOT_TICKET_TYPE_ADULT"){
                                return "成人票";
                            }else if (type==="D_SPOT_TICKET_TYPE_CHILDREN")
                                return "儿童票"
                        }},
                    {name:"所属景区",field:"spotName",width:"10%"}
                ],
                toolbar:[
                    {name:"添加",icon:"fa fa-plus-circle",callback:function(event){
                            saveOrUpdate(null);
                        }},
                    {name:"删除",icon:"fa fa-search",callback:function(event){
                            deleteBySelectIds();
                    }},
                    {name:"查询",icon:"fa fa-search",callback:function(event){
                            var querySpotId = $("#querySpotId").val();
                            if(""===querySpotId.trim()){
                                selectAll();
                            }else if (""!=querySpotId){
                                selectTicket();
                            }
                        }}
                ],
                data:data
            });
        };
        var datas ={};
        var selectAll = function () {
            $.ajax({
                url:getServer()+"/flea/hclv/spotTicket/selectByDeptId.htmls",
                success:function (data) {
                    if (data.status != 200) {
                        return;
                    }
                    // console.log(data.curPageData);
                    datas=data.curPageData;
                    initGrid(datas);
                }
            });
        };
        var saveOrUpdate = function(row){
            slidebar = Slidebar({
                size:"lg",
                url : getServer() + "/static/app/spots/spotsTicket/views/editSpotsTicket2.html",
//                close:true,
                afterLoad : function($Panel) {
                    var $form = $("#spotTicketForm");

                    if(row==null){
                        getSpots(null);
                        getTicketType(null);
                    }else {
                        getTicketType(row.type);
                        getSpots(row.uuid);
                    }
                    Tool.deserialize($form, row);
                    //绑定保存按钮
                    $("#btnSave").on("click",function() {
                        saveOrUpdateSpot();
                    });
                }
            })
        };
        //获取门票类型
        var getTicketType = function (type) {
            $.ajax({
                url:getServer()+"/flea/dict/initTree.htmls",
                async:false,
                data:{'dictCode':'D_SPOT_TICKET_TYPE'},
                success:function(data){
                    queryTicketTypeSelect(data,type);
                }
            });
        };
        //类型下拉框
        var queryTicketTypeSelect = function(types,initType){
            inputSelect.dropCache();
            inputSelect({
                id:"ticketTypeName",
                key : {
                    id : "dictCode",
                    name : "dictName",
                    data : "data"
                },
                data :types,
                searchAble : false,
                initData:[initType],
                callback : function(id, data) {
                    var str = "";
                    $.each(data, function(i, n){
                        str += n.dictCode;
                    });
                    $("#type").val(str);
                }
            });
        };
        //获取景区数据
        var getSpots = function (initSpot) {
            $.ajax({
                url:getServer()+"/flea/hclv/spot/selectAllSpots.htmls",
                success:function (data) {
                    if (data.status != 200) {
                        return;
                    }
                    data = data.data;
                    getSpotsSelect(data,initSpot)
                }
            })
        };
        //景区下拉框
        var getSpotsSelect = function(spots,initSpot){
            inputSelect.dropCache();
            inputSelect({
                id:"spotName",
                key : {
                    id : "id",
                    name : "name",
                    data : "data"
                },
                data :spots,
                searchAble : false,
                initData:[initSpot],
                callback : function(id, data) {
                    var str = "";
                    $.each(data, function(i, n){
                        str += n.id;
                    });
                    $("#spotId").val(str);
                }
            });
        };
        //保存数据
        var saveOrUpdateSpot = function() {
            var model = Tool.serialize("spotTicketForm"); //序列化表单1
            $.ajax({
                url: getServer() + "/flea/hclv/spotTicket/saveOrUpdateSpotTicket.htmls",
                data:
                    {"model": JSON.stringify(model)},
                success: function (data) {
                    if (data.status != 200) {
                        return;
                    }
                    Util.alert(data.message);
                    //todo 保存后页面不刷新
                    selectAll();
                    tgrid.refresh();
                    slidebar.close();
                }
            });
        };
        //删除数据
        var deleteBySelectIds = function(){
            var gdata = Grid.getGrid("DemoGirdDivId");
            var selectRows = gdata.getSelectedRow();
            if(null!=selectRows){
                Util.confirm("是否要删除", function() {

                    var ids = [];

                    $.each(selectRows,function( index, selectRow ) {
                        ids.push(selectRow.id);
                    });

                    $.ajax({
                        url:getServer()+"/flea/hclv/spotTicket/deleteByIds.htmls",
                        method:'post',
                        data:{'ids':JSON.stringify(ids)},
                        success:function(data){
                            if (data.status != 200) {
                                return;
                            }
                            selectAll();
                            Util.alert(data.message);

                        }
                    });

                }, function() {

                });
            }else{
                Util.alert('请选择要删除的数据！');
            }
        };
        //获取景区数据2
        var querySpots = function (initSpot) {
            $.ajax({
                url:getServer()+"/flea/hclv/spot/selectAllSpots.htmls",
                success:function (data) {
                    if (data.status != 200) {
                        return;
                    }
                    data = data.data;
                    querySpotsSelect(data,initSpot)
                }
            })
        };
        //景区下拉框2
        var querySpotsSelect = function(spots,initSpot){
            inputSelect.dropCache();
            inputSelect({
                id:"querySpotName",
                key : {
                    id : "id",
                    name : "name",
                    data : "data"
                },
                data :spots,
                searchAble : false,
                initData:[initSpot],
                callback : function(id, data) {
                    var str = "";
                    $.each(data, function(i, n){
                        str += n.id;
                    });
                    $("#querySpotId").val(str);
                }
            });
        };
        var selectTicket = function () {
          var querySpotId = $("#querySpotId").val();
          $.ajax({
              url:getServer()+"/flea/hclv/spotTicket/selectByDeptId.htmls",
              data:{"spotId":querySpotId},
              success:function (data) {
                  if (data.status != 200) {
                      return;
                  }
                  initGrid(data.curPageData);
              }
          })
        };






            return {
                init:init
            }
    });